package info.niwota.ziplock;

import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.text.TextUtils;

/**
 * @author qiangli
 * 
 */
public final class Preferences {
	private static final String PREF_LOCK_HASH = "lock.hash";

	private static final String PREF_LOCK_KEY = "lock.key";

	private static final String PREF_LOGIN_TOGGLE = "settings.loggedin.toggle";

	private static final String PREF_AUTO_LOGOUT = "settings.auto.logout";

	private static final String PREF_VIEW_DEFAULT = "settings.view.default";

	private static final String SEP = "\n";

	public static final String EULA = "ziplock.eula";

	public static final String EULA_ACCEPTED = "ziplock.eula.accepted";

	public static final int VIEW_LIST = 1;

	public static final int VIEW_THUMBNAILS = 2;

	public static String get(Context ctx, String key) {
		final SharedPreferences p = PreferenceManager
				.getDefaultSharedPreferences(ctx);
		return p.getString(key, null);
	}

	private static String[] getKey(Context ctx, String key) {
		try {
			final SharedPreferences p = PreferenceManager
					.getDefaultSharedPreferences(ctx);
			String s = p.getString(key, null);
			if (!TextUtils.isEmpty(s)) {
				String[] a = s.split(SEP);
				if (a.length != 3) {
					return null; // invalid
				}
				return a;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public static String getLockHash(Context ctx) {
		return get(ctx, PREF_LOCK_HASH);
	}

	public static String[] getLockKey(Context ctx) {
		return getKey(ctx, PREF_LOCK_KEY);
	}

	public static boolean isLoggedIn(Context ctx) {
		final boolean defvalue = false;
		try {
			final SharedPreferences pref = PreferenceManager
					.getDefaultSharedPreferences(ctx);
			return pref.getBoolean(PREF_LOGIN_TOGGLE, defvalue);
		} catch (Exception e) {
		}
		return defvalue;
	}

	public static void put(Context ctx, String key, String s) {
		final SharedPreferences p = PreferenceManager
				.getDefaultSharedPreferences(ctx);
		p.edit().putString(key, s).commit();

	}

	private static void putKey(Context ctx, String key, String[] values) {
		final SharedPreferences p = PreferenceManager
				.getDefaultSharedPreferences(ctx);
		StringBuilder sb = new StringBuilder();
		if (values != null) {
			for (int i = 0; i < values.length; i++) {
				sb.append(values[i]);
				sb.append(SEP);
			}
		}
		p.edit().putString(key, sb.toString()).commit();
	}

	public static void putLockHash(Context ctx, String s) {
		put(ctx, PREF_LOCK_HASH, s);
	}

	public static void putLockKey(Context ctx, String[] s) {
		putKey(ctx, PREF_LOCK_KEY, s);
	}

	public static void setLoggedIn(Context ctx, boolean b) {
		final SharedPreferences p = PreferenceManager
				.getDefaultSharedPreferences(ctx);
		p.edit().putBoolean(PREF_LOGIN_TOGGLE, b).commit();
	}

	public static int getDefaultView(Context ctx) {
		final int dv = 1; // list
		try {
			final SharedPreferences pref = PreferenceManager
					.getDefaultSharedPreferences(ctx);
			String s = pref.getString(PREF_VIEW_DEFAULT, null);
			if (s == null) {
				return dv;
			}
			int v = Integer.parseInt(s);
			return (v == 2 ? v : 1); // only supports 1 or 2 for now
		} catch (Exception e) {
			e.printStackTrace();
		}
		return dv;
	}

	public static int getAutoLogut(Context ctx) {
		int dv = 15;
		try {
			final SharedPreferences pref = PreferenceManager
					.getDefaultSharedPreferences(ctx);
			String s = pref.getString(PREF_AUTO_LOGOUT, null);
			if (s == null) {
				return dv;
			}
			int v = Integer.parseInt(s);
			return (v > 0 && v <= 60 ? v : dv);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return dv;
	}

	private Preferences() {
	}
}
